package moh.s2.s2dao.impl;

import java.util.List;

import moh.s2.s2dao.AsDao;
import moh.s2.s2entity.AppSettingVO;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;


public class AsDaoImpl implements AsDao {

	Session session;
	
	@Autowired
	@Qualifier("sessionFactory1")
	private SessionFactory sessionFact; 
	
	
	
	
	public AsDaoImpl() {		System.out.println("AsDaoImpl  constructor");
		//BasicConfigurator.configure();
	
		//ApplicationContext ctx = new ClassPathXmlApplicationContext("appsetting-dao.xml");
		//if (ctx == null) System.out.println("AsDaoImpl ctx == null");
		//sessionFact = (SessionFactory)ctx.getBean("sessionFactory1");
	
	
		//session = sessionFact.openSession();
		
	
        try {
        	
        	//ApplicationContext ctx = new ClassPathXmlApplicationContext("hibernate.cfg.xml");
        	//File f = new File("C:\\java\\apps-ashiq\\hibernate.cfg.xml");
        	//SessionFactory sessionFactory = new Configuration().configure(f).buildSessionFactory();
         	
        	//this.getClass().getResourceAsStream(name)
        	//SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    		//session = sessionFactory.openSession();
    		
    		//Transaction tx = null;
	         //tx = session.beginTransaction();
	         ///ID = (Integer) session.save(patient); 
	         //tx.commit();
        
        } catch (HibernateException e) {
        	//if (tx!=null) tx.rollback();
        	e.printStackTrace(); 
       
        } catch (Exception ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            //throw new ExceptionInInitializerError(ex);

	      }finally {
	         //session.close(); 
	      }
	     
	}
	

	public SessionFactory getSessionFact() {
		return sessionFact;
	}


	public void setSessionFact(SessionFactory sessionFact) {
		this.sessionFact = sessionFact;
	}

	@Override
	public List<AppSettingVO> getMainIds() {
		
		
		 System.out.println("getMainIds");
		 
		List<AppSettingVO> lst = null;
		/*String hql = "distinct mainId from AppSettingVO";
		//session = sessionFact.openSession();
		//session.createQuery(hql);
		
		String hql = "select count(as.mainId) FROM AppSettingVO as"; 
		Query query = session.createQuery(hql); 
		if (query == null)
			System.out.println("Query is null");
		List<Integer> results = query.list();
		for(Integer s:results)
			System.out.println(s);
		*/
		return lst;
	}

	@Override
	public AppSettingVO get(int id) {
		System.out.println("AsDao.get(int id)");
		AppSettingVO vo = null;
		session = sessionFact.openSession();
		try {
			Criteria ct = session.createCriteria(AppSettingVO.class);
			ct.add(Restrictions.like("id", id));
			List<AppSettingVO> lst = (List<AppSettingVO>)ct.list();
			if (lst != null)
				vo = lst.get(0);
			//lst = Collections.checkedList(ct.list(), AppSettingVO.class);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		return vo;
	}
	
	
	
	@Override
	public List<AppSettingVO> getAppSettings() {
		System.out.println("AsDao.getAppSettings()");
		session = sessionFact.openSession();
		List<AppSettingVO> lst = null;
		try {
			Criteria ct = session.createCriteria(AppSettingVO.class);
			ct.addOrder( Property.forName("mainId").asc() );
		    ct.addOrder( Property.forName("subId").desc() );
			lst = (List<AppSettingVO>)ct.list();
			//lst = Collections.checkedList(ct.list(), AppSettingVO.class);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

		return lst;
	}

	

	@Override
	public List<AppSettingVO> getAppSettings(String mainId) {
		
		System.out.println("AsDao. getAppSettings(String mainId) " + mainId);
		session = sessionFact.openSession();
		List<AppSettingVO> lst = null;
		try {
			System.out.println("AsDao.getAppSettings " + mainId);
			Criteria ct = session.createCriteria(AppSettingVO.class);
			
			ct.add(Restrictions.like("mainId", mainId + "%"));
			
			lst = ct.list();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return lst;
	}

	@Override
	public AppSettingVO getAppSetting(String mainId, String subId) {
		System.out.println("AsDao.getAppSetting(String mainId, String subId)");
		session = sessionFact.openSession();
		List<AppSettingVO> lst = null;
		AppSettingVO vo = null;
		try {
			Criteria ct = session.createCriteria(AppSettingVO.class);
			ct.add(Restrictions.like("mainId", "Translation"));
			ct.add(Restrictions.like("subId", "Arabic"));
			lst = ct.list();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		if (lst.size()>0) 
			vo = lst.get(0);
		return vo;
	}

	//@Override
	//public List<AppSettingVO> getAppSettings(String paramName, String value) {
	//	// TODO Auto-generated method stub
	//	return null;
	//}
	
	public List<AppSettingVO> getAppSettings(AppSettingVO appSettingVO) {
		
		System.out.println("AsDao.getAppSettings AppSettingVO " + appSettingVO);

		session = sessionFact.openSession();
		
		return null;
	}
	
	

	@Override
	public AppSettingVO add(AppSettingVO appSettingVO) {
		session = sessionFact.openSession();
		System.out.println("here here here");
		AppSettingVO ret = null;
		try {
	 	
	    		System.out.println("begin");	
				Transaction tx = session.beginTransaction();
		        int ID = (Integer) session.save(appSettingVO);
		        System.out.println("sava settings");
		        //session.getTransaction().commit();
		        tx.commit();
		        //session.flush();
		        ret = appSettingVO;
		        System.out.println("AppSettingVO add(AppSettingVO appSettingVO)");
		        
	        } catch (HibernateException e) {
	        	//if (tx!=null) tx.rollback();
	        	e.printStackTrace(); 
	       
	        } catch (Exception ex) {
	        	System.err.println("Initial SessionFactory creation failed." + ex);
	        	ex.printStackTrace(); 
	            
	            //throw new ExceptionInInitializerError(ex);

		      }
		return ret;
	}

	@Override
	public boolean delete(String mainId, String subId) {
		session = sessionFact.openSession();
		AppSettingVO vo = getAppSetting(mainId, subId);
		if (vo != null) {
			Transaction tx = session.beginTransaction();
			session.delete(vo);
			tx.commit();
		}
			
		
		return vo != null ? true : false;
	}

	@Override
	public boolean delete(int id) {
		boolean ret = false;
		session = sessionFact.openSession();
		AppSettingVO vo = get(id);
		if (vo != null) {
			Transaction tx = session.beginTransaction();
			session.delete(vo);
			tx.commit();
			ret = true;
		}
		session.close();
		return ret;
	}
	
	
	@Override
	public AppSettingVO update(AppSettingVO appSettingVO) {
		session = sessionFact.openSession();
		
		AppSettingVO vo = null;
		try {
			if (appSettingVO != null) {
				Transaction tx = session.beginTransaction();
				session.update(appSettingVO);
				tx.commit();
				vo = appSettingVO;
			}
			
		} catch(Exception ex) {
		}
				
		return vo;
	}


}
